package com.basicsOperation.controller;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.basicsOperation.service.BasicsOperationService;
import com.basicsOperation.vo.PathVo;
import com.basicsOperation.vo.UserInfoVo;
import com.util.GlobalUtil;

@RequestMapping("login")
@Controller
public class LoginController {
	
	@Autowired
	private BasicsOperationService basicsOperationServiceImp;
	
	//log4j日志
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	//首页跳转
	@RequestMapping("jumpHomePage")
	public String jumpHomePage(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map=new HashMap<String, String>();
		 map.put("userName", request.getParameter("userName"));
		 map.put("userPass", request.getParameter("userPass"));
		 List<PathVo> pathInfo=basicsOperationServiceImp.selPathInfo(map);
		 HttpSession session=request.getSession();
		   session.setAttribute("pathInfo", pathInfo);
		return "homePage.jsp";
	}
	
	//登陆验证
	@RequestMapping("loginVerification")
	@ResponseBody
	public String loginVerification(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{	
		   
		   String checkCode=request.getParameter("checkCode");
		   String verifyCode=(String) request.getSession().getAttribute("VerifyCode");
		   //判断验证码是否正确
		   if (verifyCode.equals(checkCode)) {
			   String userName=request.getParameter("userName");
			   String userPass=request.getParameter("userPass");
			   Map<String, String> map= new HashMap<String, String>();
			      map.put("userName", userName);
			      map.put("userPass", userPass);			      
			  List<UserInfoVo> userInfo=basicsOperationServiceImp.selUserInfo(map);
				 if (userInfo.size()==1) {
					 //cookie发给客户端
					 sendAutoLoginCookie(request,response,userInfo.get(0));
					 request.getSession().setAttribute(GlobalUtil.ADMIN_IN_SESSION,userInfo);
					 
					System.out.println("111111111111111111111"+System.getProperty("user.dir")); 
					 return "1";//登陆成功
				  } else {
					 return "0"; //账号或密码错误
				  }
		   }else{			   
			   return "2";//验证码错误
		   }		   		  		 		 		 
	}				
	  /**
	    * @Method: sendAutoLoginCookie(自动登录)
	    * @Description: 发送自动登录cookie给客户端浏览器
	    * @param request
	    * @param response
	    * @param user
	 * @throws UnsupportedEncodingException 
	    */ 
	    private void sendAutoLoginCookie(HttpServletRequest request, HttpServletResponse response, UserInfoVo user) throws UnsupportedEncodingException {
	    	//得到保存cookie的时间
	    	if (request.getParameter("logintime")!=null) {
	            int logintime = Integer.parseInt(request.getParameter("logintime"));
	            //创建cookie,cookie的名字是autologin，值是用户登录的用户名和密码，用户名和密码之间使用.进行分割，密码经过md5加密处理
	                                                 //处理中文名
	            Cookie cookie = new Cookie("autologin",URLEncoder.encode(user.getLoginName(),"UTF-8") + "." + user.getLoginPass());
	            //设置cookie的有效期
	            cookie.setMaxAge(logintime);
	            //设置cookie的有效路径
	            cookie.setPath(request.getContextPath());
	            //将cookie写入到客户端浏览器
	            response.addCookie(cookie);
	        }	    		    	
	    }
	    
	    /**
	     * @Method: removeAutoLoginCookie(登出时,删除cookie)
	     * @Description: 删除自动登录cookie，
	     *     JavaWeb中删除cookie的方式就是新创建一个cookie，新创建的cookie与要删除的cookie同名，
	     *     设置新创建的cookie的cookie的有效期设置为0，有效路径与要删除的cookie的有效路径相同
	     * @param request
	     * @param response
	     */ 
	     private void removeAutoLoginCookie(HttpServletRequest request, HttpServletResponse response) {
	         //创建一个名字为autologin的cookie
	         Cookie cookie = new Cookie("autologin","");
	         //将cookie的有效期设置为0，命令浏览器删除该cookie
	         cookie.setMaxAge(0);
	         //设置要删除的cookie的path
	         cookie.setPath(request.getContextPath());
	         response.addCookie(cookie);
	     }
	  
}
